package com.test.jdk.streamApi;


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 结果收集到集合中:
 * collect使用
 */
public class StreamResultCollenction {
    public static void main(String[] args) {

        //收集到list中
        List<String> collect = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toList());
        collect.forEach(System.out::println);
        System.out.println(collect);
        //收集到set集合总共
        Set<String> set = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toSet());
        set.forEach(System.out::println);
        System.out.println(set);
        //如果需要获取的类型为具体的实现，比如：ArrayList HashSet
        ArrayList<String> collect1 = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toCollection(ArrayList::new));
        collect1.forEach(System.out::println);
        System.out.println(collect1);
        HashSet<String> hashSet = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toCollection(HashSet::new));
        hashSet.forEach(System.out::println);
        System.out.println(hashSet+"===============================");

        //将结果收集在数组中
        Object[] objects = Stream.of("aa", "bb", "cc", "dd").toArray();
        System.out.println(Arrays.toString(objects));
        //如果我们需要指定返回的数组中的元素类型
        String[] strings = Stream.of("aa", "bb", "cc", "aa").toArray(String[]::new);
        System.out.println(Arrays.toString(strings));
    }
}
